package com.ctrl.riveradministrator.mvp.view.activity.trace.statistics;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.mvp.model.Reach;
import com.ctrl.riveradministrator.mvp.presenter.StatisticsPresenter;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.mapapi.BMapManager.getContext;

public class StatisticsActivity extends BaseActivity<StatisticsPresenter> implements StatisticView {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private Spinner spinner;
    private String sk = "-1";
    private ProgressBar bar;

    private RecyclerView rl;
    private LinearLayoutManager layoutManager;
    private StatisticsAdapter statisticsAdapter;
    private List<String> name;
    private ArrayAdapter adapter;
    private List<Reach> reaches;

    /**
     * 初始化bundle参数
     *
     * @param parms parms
     */
    @Override
    protected void initParams(Bundle parms) {

    }

    /**
     * 绑定布局
     *
     * @return 布局ID
     */
    @Override
    protected int bindLayout() {
        return R.layout.activity_statistics;
    }

    /**
     * 初始化控件
     *
     * @param view view
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        spinner = $(R.id.sp);
        bar = $(R.id.probar);
        rl = $(R.id.rl);

        name = new ArrayList<>();
        reaches = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
        spinner.setAdapter(adapter);

        layoutManager = new LinearLayoutManager(getContext());
        rl.setLayoutManager(layoutManager);
        statisticsAdapter = new StatisticsAdapter(this);
        rl.setAdapter(statisticsAdapter);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText("巡河统计");
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    /**
     * 业务操作
     *
     * @param mContext context
     */
    @Override
    protected void doBusiness(Context mContext) {
        bar.setVisibility(View.VISIBLE);
        mPresenter.getReach();
    }

    /**
     * View点击
     *
     * @param view view
     */
    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    /**
     * 设置监听
     */
    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sk = reaches.get(position).getId();
                if (reaches.get(position).getReachName() == null) {
                    mPresenter.getStatisticsList(sk);
                } else {
                    mPresenter.getStatisticsRiverList(sk);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected StatisticsPresenter createPresenter() {
        return new StatisticsPresenter(this);
    }

    @Override
    public void onSuccess(Object model, int type) {

    }

    @Override
    public void getReach(List<Reach> objs) {
        reaches.clear();
        for (int i = 0; i < objs.size(); i++) {
            name.add(objs.get(i).getReachName());
            reaches.add(objs.get(i));
        }
        mPresenter.getReservoirList(RaApplication.getUserId());
    }

    @Override
    public void getReservoir(List<Reach> objs) {
        for (int i = 0; i < objs.size(); i++) {
            name.add(objs.get(i).getReservoirName());
            reaches.add(objs.get(i));
        }
        adapter.notifyDataSetChanged();
        try {
            sk = reaches.get(0).getId();
        } catch (Exception e) {
            sk = "-1";
        }
        bar.setVisibility(View.GONE);
    }

    @Override
    public void getStatistic(List<Reach> objs) {
        statisticsAdapter.updateListView(objs);
    }

    @Override
    public void onFailed(String message) {

    }
}